<?php

namespace App\Http\Controllers\Guard\Car;

use App\Http\Controllers\Common\GuardController;
use App\Models\Car\CarInfo;
use App\Models\Constant\CarConstant;
use Carbon\Carbon;
use Illuminate\Http\Request;

class CarInfoController extends GuardController{


    public function __construct() {
        $this->instance = new CarInfo();
    }

    public function lst(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);
        foreach ($records as $record) {
            $record->rent_unit_name = $record->getRentUnit();
        }
        return responseSuccess($records->items(),$records->total());
    }

    public function insuranceLst(Request $request){
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $conditions['orderBy']['col'] = 'insurance_date';
        $conditions['orderBy']['sort'] = 'asc';
        $records = $this->instance->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);
        $today = date('Y-m-d');

        foreach ($records as $record) {
            $record->rent_unit_name = $record->getRentUnit();
            $record->insurance_warning = ($today>=Carbon::parse($record->insurance_date)->subMonth());
        }
        return responseSuccess($records->items(),$records->total());
    }

    public function buyInsurance(Request $request){
        $id = $request->input('id',0);
        $record = $this->instance->getRecordById($id);
        $record->insurance_date = Carbon::parse($record->insurance_date)->addYear();
        $record->save();
        return responseSuccess();
    }

    public function fixLst(Request $request){
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $conditions['orderBy']['col'] = 'status';
        $conditions['orderBy']['sort'] = 'desc';
        $records = $this->instance->getLst($conditions)
            ->paginate($pageSize,$this->getCol,'page',$page);
        foreach ($records as $record) {
            $record->rent_unit_name = $record->getRentUnit();
        }
        return responseSuccess($records->items(),$records->total());
    }

    public function add(Request $request) {
        $info = $request->input();

        $this->validate($request, [

        ],[],[

        ]);

        $record = $this->instance->updateOrCreate(['id'=>$info['id']??0],$info);
        return responseSuccess($record->toArray(),1);
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $record = $this->instance->getRecordById($id);
        return responseSuccess($record->toArray(),1);
    }

    public function change(Request $request) {
        $id = $request->input('id',0);
        $status = $request->input('status');
        $this->instance->changeColById($id, compact('status'));
        return responseSuccess();
    }

    public function del(Request $request) {
        $id = $request->input('id',0);
        $this->instance->delRecordById($id);
        return responseSuccess();
    }
}
